/**
 * OSAmI: Open Source Ambient Intelligence
 * Servlets running on the gateway, for displaying data from the sensor nodes.
 * Copyright (C) Jonathan Ferreira 2010 (internship in LCIS-INPG, for a demo in the FR part of the OSAMI project)
 * Contact: osami-general@osami-commons.org
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation;
 * version 2.1 of the License.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Suite 500 Boston, MA 02110-1335 USA
 */
package org.osami.gateway.display.internal;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.felix.ipojo.annotations.Component;
import org.apache.felix.ipojo.annotations.Instantiate;
import org.apache.felix.ipojo.annotations.Invalidate;
import org.apache.felix.ipojo.annotations.Requires;
import org.apache.felix.ipojo.annotations.Validate;
import org.osami.gateway.display.internal.identity.IdentitiesServlet;
import org.osgi.service.http.HttpContext;
import org.osgi.service.http.HttpService;
import org.osgi.service.http.NamespaceException;

@Component
@Instantiate
public class IndexViewServlet extends HttpServlet {

    private static final long serialVersionUID = 1L;

    @Requires
    private HttpContext context;

    @Requires
    private HttpService httpService;

    @Validate
    public void start() throws ServletException, NamespaceException {
        this.httpService.registerServlet(ServletPaths.INDEX, this, null, this.context);

        this.httpService.registerResources("/resource", "/webContent", null);
    }

    @Invalidate
    public void stop() {
        this.httpService.unregister(ServletPaths.INDEX);

        this.httpService.unregister("/resource");
    }

    @Override
    synchronized public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
        PrintWriter writer = response.getWriter();

        writer.println("<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">");
        writer.println("<html lang=\"en\">");

        // HTML Head
        // ---------------------------------------------------
        writer.println("<head>");
        writer.println("<title>Select a list for sensors or nodes</title>");
        writer.println("<link href=\"/resource/css/designSensor.css\" rel=\"stylesheet\" type=\"text/css\" />");
        writer.println("<meta http-equiv=\"refresh\" content=\"5\">");
        writer.println("</head>");

        // HTML Body
        // ---------------------------------------------------
        writer.println("<body>");

        String html = "";
        html += "<H1>Select a list for sensors or nodes</H1><hr/>\n";

        html += "<ul>";
        html += "\t<li><a href=\"" + ServletPaths.NODES + "\">List of sensor nodes</a></li>";
        html += "\t<li><a href=\"" + ServletPaths.SENSORS + "\">List of sensors</a></li>";
        html += "\t<li><a href=\"" + ServletPaths.POWERPLUGS + "\">List of power plugs</a></li>";
        html += "\t<li><a href=\"" + IdentitiesServlet.IDENTITY + "\">Identities Management</a></li>";
        html += "\t<li><a href=\"" + ServletPaths.RULE + "\">Rules Management</a></li>";
        html += "\t<li><a href=\"" + ServletPaths.PROFILE + "\">Profile Management</a></li>";
        html += "</ul>";

        writer.println(html);

        writer.println("</body>");
        writer.println("</html>");
    }
}
